// app.js
import {Pay} from "./model/pay";

App({
  onLaunch() {},
  globalData: {
    changeColor: "#ead338",
    fontColor: "#666666",
    titleColor: "#333333",
    moreColor: "#999999",
    whiteColor: "#ffffff",
    pagesgo: "",
  },
  /**
   * 获取年月日，星期数组
   */
  getDay(day) {
    const today = new Date();
    const targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds);
    const tYear = today.getFullYear();
    let tMonth = today.getMonth();
    let tDate = today.getDate();
    let week = today.getDay();
    week = this.getStringWeek(week);
    tMonth = this.doHandleMonth(tMonth + 1);
    tDate = this.doHandleMonth(tDate);
    return [ week, tYear+"-"+tMonth+"-"+tDate]; //年月日
    // return tMonth + "-" + tDate; //月日
  },
  /**
   * 获取年月日时分格式
   */
  getDateTime(time) {
    time = new Date(time);
    const tYear = time.getFullYear();
    let tMonth = time.getMonth();
    let tDate = time.getDate();
    tMonth = this.doHandleMonth(tMonth + 1);
    tDate = this.doHandleMonth(tDate);
    let hours = time.getHours();    // 获取当前小时数(0-23)
    let minutes = time.getMinutes();   // 获取当前分钟数(0-59)
    hours = this.doHandleMonth(hours);
    minutes = this.doHandleMonth(minutes);
    return `${tYear}-${tMonth}-${tDate} ${hours}:${minutes}`
  },
  /**
   *  一位月份天数前面加0
   * @param month
   * @returns {string}
   */
  doHandleMonth(month) {
    let m = month;
    if (month.toString().length == 1) {
      m = "0" + month;
    }
    return m;
  },
  getDateWeek(e) {
    let data = [];
    for (let i = 0; i < e; i++) {
      data.push(this.getDay(i));//-i 代表之前  i代表将来
    }
    return data
  },
  /**
   * 获取日期
   * @param num 星期数字类型
   * @returns {string} 星期字符类型
   */
  getStringWeek(num) {
    const week = [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六"
    ]
    return week[num];
  },
  /**
   * banner点击跳转
   * @param e 点击对象
   */
  bannerLinkTo(e){
    if(e.target.dataset.type == 1) {
      wx.navigateTo({
        url: `banner?id=${e.target.dataset.id}`,
      })
    } else if(e.target.dataset.type == 2) {
      wx.navigateTo({
        url: `../activity/official?id=${e.target.dataset.typeid}`
      })
    } else if(e.target.dataset.type == 3) {
      wx.navigateTo({
        url: `../activity/business_info?id=${e.target.dataset.typeid}`
      })
    } else if(e.target.dataset.type == 4) {
      wx.navigateTo({
        url: `../activity/personal?id=${e.target.dataset.typeid}`
      })
    }
  },

  /**
   * 首页活动跳转
   */
  activityLinkTo(e) {
    if(e.currentTarget.dataset.type == 1) {
      wx.navigateTo({
        url: `../activity/official?id=${e.currentTarget.dataset.typeid}`
      })
    } else if(e.currentTarget.dataset.type == 2) {
      wx.navigateTo({
        url: `../activity/business?id=${e.currentTarget.dataset.typeid}`
      })
    } else if(e.currentTarget.dataset.type == 3) {
      wx.navigateTo({
        url: `../activity/personal?id=${e.currentTarget.dataset.typeid}`
      })
    }
  },
  /**
   * 支付公用 - 获取支付信息
   */
  async preOrder(out_trade_no, cb) {
    const res = await Pay.preOrder({ out_trade_no:out_trade_no, payment: 1 });
    if(res.pay_type == 1) {
      this.transferPay(res,cb);
    } else if(res.pay_type == 2) {
      this.successPay("支付成功");
      cb();
    } else {
      cb();
    }
  },
  /**
   * 调起支付信息
   */
  transferPay(res,cb) {
    const self = this;
    wx.requestPayment({
      timeStamp: res.timeStamp.toString(),
      nonceStr: res.nonceStr,
      package: res.package,
      signType: res.signType,
      paySign: res.paySign,
      success: function (res) {
        self.successPay("支付成功");
        cb();
      },
      fail: function (error) {
        if (error.errMsg == "requestPayment:fail cancel") {
          wx.showToast({
            title: '取消了支付',
            icon: 'none',
            duration: 2000
          })
          cb();
          return false
        } else {
          wx.showToast({
            title: '支付错误',
            icon: 'none',
            duration: 2000
          })
          cb();
        }
      }
    })
  },
  /**
   * 支付成功弹框
   */
  successPay(name) {
    const self = this;
    wx.showModal({
      title: '温馨提示',
      content: name,
      showCancel: false,
      confirmText: '知道了',
      confirmColor: self.globalData.changeColor,
      success:function(res){
        if (res.confirm){
          wx.switchTab({
            url: self.globalData.pagesgo,
          })
        }
      }
    })
  },
})
